//
//  FFRechargeMoneyGridCell.m
//  LLWallet
//
//  Created by tt on 2020/9/15.
//

#import "FFRechargeMoneyGridCell.h"

@implementation FFRechargeMoney

+(instancetype) initWith:(NSString*)title money:(NSNumber*)money {
    FFRechargeMoney *m = [FFRechargeMoney new];
    m.title = title;
    m.money = money;
    return m;
}

@end

@implementation FFRechargeMoneyGridModel

- (Class)cell {
    return FFRechargeMoneyGridCell.class;
}

@end

@interface FFRechargeMoneyGridCell ()

@property(nonatomic,strong) UIView *boxView;
@property(nonatomic,strong) FFRechargeMoneyGridModel *model;

@end

#define eachLineNum 3 // 每行item数量
#define eachItemheight 60.0f
#define hItemSpace 15.0f // 横的item的间距
#define vItemSpace 15.0f // 竖的item的间距
@implementation FFRechargeMoneyGridCell


+ (CGSize)sizeForModel:(FFRechargeMoneyGridModel *)model {
    CGFloat height = 0.0f;
    if(model.items && model.items.count>0) {
       CGFloat count = model.items.count/eachLineNum;
        if(model.items.count%eachLineNum !=0) {
            count = count + 1;
        }
        height = (eachItemheight+vItemSpace) * count + vItemSpace;
    }

    return CGSizeMake(WKScreenWidth, height);
}

- (void)setupUI {
    [super setupUI];
    [self setBackgroundColor:[UIColor clearColor]];
    [self.contentView addSubview:self.boxView];
}

- (void)refresh:(FFRechargeMoneyGridModel *)model{
    [super refresh:model];
    self.model = model;
    
    [self.boxView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    if(model.items && model.items.count>0) {
        for (NSInteger i=0; i<model.items.count; i++) {
            FFRechargeMoney *item = model.items[i];
            FFRechargeMoneyView *itemView = [[FFRechargeMoneyView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, 0.0f, eachItemheight)];
            itemView.titleLbl.text = item.title;
            [self.boxView addSubview:itemView];
            itemView.tag = i;
            [itemView addTarget:self action:@selector(itemClick:) forControlEvents:UIControlEventTouchUpInside];
            
            if(model.value && model.value.floatValue>0) {
                if(item.money.floatValue == model.value.floatValue) {
                    [self selected:itemView];
                }
            }
        }
        
    }

}

-(void) selected:(FFRechargeMoneyView*)view {
    [view setBackgroundColor:[WKApp shared].config.themeColor];
    [view.titleLbl setTextColor:[UIColor whiteColor]];
}

-(void) itemClick:(FFRechargeMoneyView*)view {
    NSInteger index = view.tag;
    FFRechargeMoney *itemModel = self.model.items[index];
    if(self.model.onItemClick) {
        self.model.onItemClick(itemModel);
    }
    
    for (FFRechargeMoneyView *oview in self.boxView.subviews) {
        [oview setBackgroundColor:[UIColor clearColor]];
        [oview.titleLbl setTextColor:[WKApp shared].config.defaultTextColor];
    }
    
   
}

- (UIView *)boxView {
    if(!_boxView) {
        _boxView = [[UIView alloc] init];
        [_boxView setBackgroundColor:[UIColor clearColor]];
    }
    return _boxView;
}


- (void)layoutSubviews {
    [super layoutSubviews];
    self.boxView.frame = self.bounds;
    NSArray *itemViews = [self.boxView subviews];
    if(itemViews) {
        CGFloat itemWidth = (self.lim_width-(hItemSpace*(eachLineNum+1)))/eachLineNum ;
        NSInteger lineIndex = -1;
        NSInteger lineStart = 0;
        for (NSInteger i=0; i<itemViews.count; i++) {
            lineStart++;
            if( i%eachLineNum == 0) {
                lineIndex++;
                lineStart = 0;
            }
            
            UIView *itemView = itemViews[i];
            itemView.lim_width = itemWidth;
            itemView.lim_left = lineStart*itemWidth + (lineStart+1)*hItemSpace;
            
            itemView.lim_top = eachItemheight * lineIndex + (lineIndex+1)*hItemSpace;
            
            
        }
    }
}


@end




@implementation FFRechargeMoneyView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.titleLbl];
        self.layer.masksToBounds = YES;
        self.layer.cornerRadius = 4.0f;
        self.layer.borderColor = [UIColor colorWithRed:200.0f/255.0f green:200.0f/255.0f blue:200.0f/255.0f alpha:1.0f].CGColor;
        self.layer.borderWidth = 1.0f;
    }
    return self;
}
- (UILabel *)titleLbl {
    if(!_titleLbl) {
        _titleLbl = [[UILabel alloc] init];
        [_titleLbl setFont:[[WKApp shared].config appFontOfSizeMedium:16.0f]];
        [_titleLbl setTextAlignment:NSTextAlignmentCenter];
        [_titleLbl setTextColor:[WKApp shared].config.defaultTextColor];
    }
    return _titleLbl;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    self.titleLbl.lim_size = self.lim_size;
}

@end
